<template>
  <div class="login-page">
    <div class="particles">
      <vue-particles
        color="#dedede"
        shapeType="star"
        linesColor="#dedede"
        hoverMode="grab"
        clickMode="push"
        style="height: 100%"
      ></vue-particles>
    </div>

    <div class="login-container" width="30%">
      <div class="logo">我们的鱼塘</div>
      <el-form
        :model="loginForm"
        ref="loginForm"
        :rules="loginRules"
        label-position="left"
        label-width="80px"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

  <style scoped>
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-container {
  position: relative; /* 添加相对定位 */
  z-index: 2; /* 设置一个大于 .particles 的 z-index 值 */
  width: 400px;
  padding: 30px;
  border-radius: 10px;
  box-shadow: 0 6px 19px rgba(0, 0, 0, 0.1);
  margin-left: 70em;
  background-color: white;
}

.logo {
  font-size: 24px;
  margin-bottom: 20px;
  text-align: center;
}

.login-form {
  text-align: left;
}
.particles {
  position: absolute;
  top: 0;
  left: 0;
  height: 100vh;
  background-image: url("../../assets/background2.jpg");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  width: 100%;
  z-index: 1;
  /* background: radial-gradient(
    ellipse at top left,
    rgba(105, 155, 200, 1) 0%,
    rgba(181, 197, 216, 1) 57%
  ); */
}
</style>
     <script>
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          {
            required: true,
            message: "Please input your username",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "Please input your password",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handleLogin() {
      // 实现登录逻辑，例如调用 API 或验证用户输入
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$axios
            .post("/user/login", this.loginForm)
            .then((response) => {
              if (response.data.data) {
                const token = response.data.data.token;
                localStorage.setItem("token", token);
                this.$router.push("/main");
              }
            })
            .catch(() => {
              this.$message.error("用户名或密码错误！");
              // this.loading = false;
            });
          console.log("Login form submitted:", this.loginForm);
          // TODO: Implement actual login logic here
        } else {
          console.log("Login form validation failed.");
        }
      });
    },
  },
};
</script>